package com.dbf.forum.common.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

@Repository(value="commonDao")
@Transactional
public class CommonDaoJpa implements CommonDao{

	@PersistenceContext
	protected EntityManager em;
	

    public <T> T create(final T t) {
        this.em.persist(t);
        return t;
    }

    public <T> void delete(Class<? extends T> clazz, final Object id) {
        this.em.remove(this.em.getReference(clazz, id));
    }

    public <T> T find(Class<? extends T> clazz, final Object id) {
        return (T) this.em.find(clazz, id);
    }

    public <T> T update(final T t) {
        return this.em.merge(t);    
    }
    
    @SuppressWarnings("unchecked")
	public <T> TypedQuery<T>/*<? extends T>*/ createQuery(Class<? extends T> clazz, String hql){
    	 return (TypedQuery<T>) this.em.createQuery(hql, clazz);
    }
    
    public Query createQuery(String hql){
   	 return this.em.createQuery(hql);
   }

}
